<?php
if(!$login_do) include("config.php");
if($_POST['session_id']){
	//////////////////////////////////////////////////////////////////////////////////////
	//获取姓名学号
	$snoopy1 = new Snoopy;
	//模拟HEADER
	$snoopy1->cookies['ASP.NET_SessionId'] = $_SESSION["ASP_NET_SessionId"];
	//获取分析表单
	$snoopy1->fetch($personurl);	
	//超时返回
	if ($snoopy1->status != '200' || $snoopy1->timed_out) {
    	echo json_err(1,"页面获取超时！");
    	return;
	}
	//转码以便查找错误
	$snoopy_results = mb_convert_encoding($snoopy1->results, 'utf-8', 'gbk');
	if(stripos($snoopy_results,"您正查看的此页已过期")){
    	echo json_err(5,"session_id错误或超时！");
    	return;
	}

	$res1 = $snoopy1->results;
	$res1_html = new simple_html_dom();
	$res1_html->load(iconv('GB2312', 'UTF-8', $res1));
	$res1_tds = $res1_html->find('td');
	$xh = strip_tags($res1_tds[2]->innertext);
	$xm = strip_tags($res1_tds[4]->innertext);
	$res1_html->clear();
	$table['ct_info'] = array(
		'xm'=>$xm,
		'xh'=>$xh,
	);

	//先获取课表查询表单，分析其中字段
	$snoopy2 = new Snoopy;
	//模拟HEADER
	$snoopy2->cookies['ASP.NET_SessionId'] = $_SESSION["ASP_NET_SessionId"];
	//获取分析表单
	$snoopy2->fetchform($tableformurl);
	//超时返回
	if ($snoopy2->status != '200' || $snoopy2->timed_out) {
    	echo json_err(1,"页面获取超时！");
    	return;
	}
	$res2 = $snoopy2->results;
	
	$xn_xq = substr($res2[2], 15, 5);
	
	$xn = substr($xn_xq, 0, 4);
	$xn = $xn.'-'.($xn*1+1);
	
	$xq = substr($xn_xq, 4, 5);
	$table['ct_info'] += array(
		'xn'=>$xn,
		'xq'=>$xq*1+1,
	);

	//获取课程表
	$snoopy3 = new Snoopy;
	$submit_vars3["Sel_XNXQ"] = $xn_xq;//学期
	$submit_vars3["rad"] = "1";//格式二	
	$submit_vars3["px"] = "0"; //按课程/环节
	$submit_vars3["Submit01"] = "检索";

	//模拟HEADER
	$snoopy3->cookies['ASP.NET_SessionId'] = $_SESSION["ASP_NET_SessionId"];
	//提交
	$snoopy3->submit($tableurl,$submit_vars3);
	//超时返回
	if ($snoopy3->status != '200' || $snoopy3->timed_out) {
    	echo json_err(1,"页面获取超时！");
    	return;
	}
	$table_info = iconv('GB2312', 'UTF-8', $snoopy3->results);
	
	if(strpos($table_info, "Sorry_note")){
		echo json_err(5,'Sorry_note!');
		return;
	}

	//noapi调用
	//if($_POST['noapi']){
		//var_dump($table_info);
		//echo $table_info;
	//	return ;
	//}

	$week_n_arr = array("一"=>1,"二"=>2,"三"=>3,"四"=>4,"五"=>5,"六"=>6,"日"=>7);
	
	$html = new simple_html_dom();
	$html->load($table_info);

	$trs = $html->find('tr.B,tr.H');
	foreach ($trs as $tr_key => $tr) {
		$tds = $tr->children;

		$title_pre = strip_tags($tds[1]->innertext);
		if($title_pre){
			$title_arr = explode(']', $title_pre);
			$title_code = substr($title_arr[0],1,strlen($title_arr[0])-1);
			$title = $title_arr[1];
		}

		$teacher_pre = strip_tags($tds[9]->innertext);
		if($teacher_pre){$teacher = $teacher_pre;}

		$learn_score_pre = strip_tags($tds[2]->innertext);
		if($learn_score_pre){$learn_score = $learn_score_pre;}

		$learn_time_pre = strip_tags($tds[3]->innertext);
		if($learn_time_pre){$learn_time = $learn_time_pre;}

		$learn_time_1_pre = strip_tags($tds[4]->innertext);
		if($learn_time_1_pre){$learn_time_1 = $learn_time_1_pre;}

		$learn_time_2_pre = strip_tags($tds[5]->innertext);
		if($learn_time_2_pre){$learn_time_2 = $learn_time_2_pre;}

		$teach_type_pre = strip_tags($tds[7]->innertext);
		if($teach_type_pre){$teach_type = $teach_type_pre;}

		$exam_type_pre = strip_tags($tds[8]->innertext);
		if($exam_type_pre){$exam_type = $exam_type_pre;}

		$type_pre = strip_tags($tds[6]->innertext);
		if($type_pre){
			$type_arr = explode('/', $type_pre);
			$type_1 = $type_arr[0];
			$type_2 = $type_arr[1];
		}

		$addr = strip_tags($tds[12]->innertext);
		$week_n_class_n = strip_tags($tds[11]->innertext);

		$week_d = substr($week_n_class_n,0,3);
		$week_n = $week_n_arr[$week_d];

		//$class_n = str_replace('-',',',substr($week_n_class_n,4,-4));
		$class_n = str_replace('节]','',$week_n_class_n);
		$class_n = str_replace('-',',',substr($class_n,4));
		
		$week_arr = explode(',', strip_tags($tds[10]->innertext));
		foreach ($week_arr as $week_key => $week) {
			$week_day = explode('-', $week);
			$week_s = $week_day[0];
			$week_e = $week_day[1];

			$table['ct_data'][$class_n][$week_n][] = array(
				'title'=>$title,
				'title_code'=>$title_code,
				'teacher'=>$teacher,
				'addr'=>$addr,
				'week_d'=>$week_d,
				'class_n'=>$class_n,
				'week_s'=>$week_s,
				'week_e'=>$week_e,
				'learn_score'=>$learn_score,
				'learn_time'=>$learn_time,
				'learn_time_1'=>$learn_time_1,
				'learn_time_2'=>$learn_time_2,
				'type_1'=>$type_1,
				'type_2'=>$type_2,
				'teach_type'=>$teach_type,
				'exam_type'=>$exam_type,
				'exam_time'=>'',
				'exam_addr'=>''
			);
		}
		
	}
	echo json_encode($table);
	$html->clear();

	//记录
	//$query = "UPDATE log SET user_id='".$_POST['UserID']."',is_ok='".$is_ok."',num='".$txt_xm."' WHERE id='".$_SESSION['insert_id']."'";
	//mysql_query($query);
}else{
	echo json_err(2,'提交数据不全!');
	return;
}
//mysql_close($link);
?>
